#include <iostream>

using namespace std;
// 111.二叉树的最小深度

// Definition for a binary tree node.
struct TreeNode {
    int val;
    TreeNode *left;
    TreeNode *right;
    TreeNode() : val(0), left(nullptr), right(nullptr) {}
    TreeNode(int x) : val(x), left(nullptr), right(nullptr) {}
    TreeNode(int x, TreeNode *left, TreeNode *right) : val(x), left(left), right(right) {}
};

class Solution {
public:
    int minDepth(TreeNode* root) {
        if(root == nullptr)
            return 0;
        if(root->left == nullptr)
            return minDepth(root->right) + 1;    // 不要忘记加1
        if(root->right == nullptr)
            return minDepth(root->left) + 1;

        //divide and conquer
        return 1 + min(minDepth(root->left), minDepth(root->right));
    }
};

int main() {
    // root = [3,9,20,null,null,15,7]
    TreeNode *tree = new TreeNode(3, new TreeNode(9), new TreeNode(20, new TreeNode(15), new TreeNode(7)));

    cout << "min="<<Solution().minDepth(tree);
    return 0;
}
